package top.huhuiyu.springboot3.base;

import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * 基础应答对象
 */
@Data
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String message;
    private Boolean success;
    private String token;
    private T data;
    public static final Integer SUCCESS = 200;
    public static final Integer ERROR = 500;

    public void setInfo(Integer code, String message, Boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    public static <T> R<T> ok(String message, T data) {
        R<T> r = new R<>();
        r.setInfo(SUCCESS, message, true, data);
        return r;
    }

    public static <T> R<T> ok(T data) {
        return ok("", data);
    }

    public static R<String> ok(String message) {
        return ok(message, null);
    }

    public static <T> R<T> fail(String message, T data) {
        R<T> r = new R<>();
        r.setInfo(ERROR, message, false, data);
        return r;
    }

    public static <T> R<T> fail(T data) {
        return fail("", data);
    }

    public static R<String> fail(String message) {
        return fail(message, null);
    }

    public static R<String> fail(Integer code, String message) {
        R<String> r = fail(message, null);
        r.setCode(code);
        return r;
    }


}
